package com.lianniu.web.tea.map.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lianniu.web.base.BaseResult;
import com.lianniu.web.baseService.area.dao.IAdminAreaMapperDao;
import com.lianniu.web.baseService.area.model.AdminAreaVo;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.tea.map.dao.TeaUserPlaceDao;
import com.lianniu.web.tea.map.model.TeaUserPlaceVo;
import com.lianniu.web.tea.map.service.ITeaUserPlaceService;
import com.lianniu.web.util.MapUtil;


@Service
@Transactional
public class TeaUserPlaceServiceImpl implements ITeaUserPlaceService {

	final static String PROVINCE_LEVEL = "2";
	public static final Logger logger = Logger.getLogger(TeaUserPlaceServiceImpl.class);

	@Resource
	private TeaUserPlaceDao teaUserPlaceDao;

	@Resource
	private IAdminAreaMapperDao adminAreaMapperDao;

	@Override
	public BaseResult getUserPlace(String userId, Float lon, Float lat) {
		logger.info("TeaUserPlaceServiceImpl getUserPlace start");
		String[] addNameCode = new String[3];
		String[] addName = new String[3];
		String parentId = "";
		logger.info("get place start");
		if (lon == null || lat == null) {
			logger.info("user refused us get place");
			if(userId==null){
				BaseResult baseResult = new BaseResult(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo, new String[3]);
				return baseResult; //用户没有登陆，且没有让获取地理位置
			}
		} else {
			addName = MapUtil.getAddName(Float.toString(lon), Float.toString(lat));
			logger.info("province:" + addName[0] + ",city:" + addName[1] + ",area:" + addName[2]);
			List<AdminAreaVo> pList = adminAreaMapperDao.getByLevel(PROVINCE_LEVEL);
			for (AdminAreaVo adminAreaVo : pList) {
				if(adminAreaVo.getAreaName().equals(addName[0])){
					addNameCode[0] = adminAreaVo.getAreaCode();
					parentId = adminAreaVo.getAreaId();
				}
			}
			List<AdminAreaVo> cList=adminAreaMapperDao.getByParentId(parentId);
			for (AdminAreaVo adminAreaVo : cList) {
				if(adminAreaVo.getAreaName().equals(addName[1])){
					addNameCode[1] = adminAreaVo.getAreaCode();
					parentId = adminAreaVo.getAreaId();
				}
			}
			List<AdminAreaVo> aList=adminAreaMapperDao.getByParentId(parentId);
			for (AdminAreaVo adminAreaVo : aList) {
				if(adminAreaVo.getAreaName().equals(addName[2])){
					addNameCode[2] = adminAreaVo.getAreaCode();
				}
			}
		}
		logger.info("get place end");
		if (userId == null) {
			logger.info("userId is null");
		} else {
			logger.info("userId is not null");
			TeaUserPlaceVo teaUserPlaceVo = teaUserPlaceDao.selectByPrimaryKey(userId);
			if (teaUserPlaceVo == null) {
				if(lon == null || lat == null){
					BaseResult baseResult = new BaseResult(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo, new String[3]);
					return baseResult;
				}else{
					logger.info("user is first");
					TeaUserPlaceVo userPlaceVo = new TeaUserPlaceVo();
					userPlaceVo.setUserId(userId);
					userPlaceVo.setLat(lat);
					userPlaceVo.setLon(lon);
					userPlaceVo.setProvinceCode(addNameCode[0]);
					userPlaceVo.setCityCode(addNameCode[1]);
					userPlaceVo.setAreaCode(addNameCode[2]);
					teaUserPlaceDao.insert(userPlaceVo);
				}
			}else{
				if(lon == null || lat == null){
					addNameCode[0] = teaUserPlaceVo.getProvinceCode();
					addNameCode[1] = teaUserPlaceVo.getCityCode();
					addNameCode[2] = teaUserPlaceVo.getAreaCode();
				}else{
					logger.info("userPlace exist");
					teaUserPlaceVo.setUserId(userId);
					teaUserPlaceVo.setLat(lat);
					teaUserPlaceVo.setLon(lon);
					teaUserPlaceVo.setProvinceCode(addNameCode[0]);
					teaUserPlaceVo.setCityCode(addNameCode[1]);
					teaUserPlaceVo.setAreaCode(addNameCode[2]);
					teaUserPlaceDao.updateByPrimaryKeySelective(teaUserPlaceVo);
				}
			}
		}
		logger.info("TeaUserPlaceServiceImpl getUserPlace end");
		return BaseResult.success("["+addNameCode[0]+"]"+addName[0]);
	}

}
